<style lang="less">
    @import '../../styles/common.less';
    @import '../tables/components/table.less';
</style>
<template>
    <div>
        <form action="" style="margin: 5px 0;overflow: hidden">
            <!--<Input v-model="name" icon="search"  placeholder="根据用户名查找" @on-click="searchName" style="width: 200px;margin-right: 3px;"></Input>-->
            <!--<Input v-model="phone" icon="search" placeholder="根据手机号查找" @on-click="searchPhone" style="width: 200px"></Input>-->

            <div style="float: right">
                <Button type="primary"  style="float: right;margin-right: 5px" @click="addAnn">添加</Button>
            </div>
        </form>
        <Table :loading="loading" refs="table1"  :data="Message" :columns="columnsList"></Table>
        <Page :total="pageNum" :page-size="5"  style="text-align: center;margin-top: 15px;" @on-change="pageChange"></Page>
    </div>
</template>
<script>
    import $http from '../../utils/axiosWrap'
    import canEditTable from '../tables/components/canEditTable.vue'
    export default {
        data() {
            return {
                loading: true,
                Message:[],
                admin:true,
                pageSize:10,
                columnsList: [
                    {
                        title: '序号',
                        width: 80,
                        align: 'center',
                        render: (h, params) => {
                            return  params.index + (this.initPage- 1) * this.pageSize + 1
                        }
                    },
                    {
                        title: '标题',
                        align: 'center',
                        key: 'biaoti',
                        editable: true
                    },
                    {
                        title: '摘要',
                        align: 'center',
                        key: 'zhaiyao'
                    },
                    {
                        title: '发布时间',
                        align: 'center',
                        width: 160,
                        render: (h, params) => {
                            var date = new Date(params.row.time);
                            return date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 140,
                        render: (h, params) => {
                            if(this.admin){
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                console.log(params)
                                                var changeObj = {
                                                    'biaoti': params.row.biaoti,
                                                    'zhaiyao': params.row.zhaiyao,
                                                    'neirong': params.row.neirong,
                                                    'pic': params.row.fengmian,
                                                    'id': params.row.id
                                                }
                                                changeObj = JSON.stringify(changeObj)
                                                localStorage.setItem('changeInner',changeObj)
                                                this.$router.push({
                                                    name: 'artical-publish'
                                                });
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.info(params)
                                            }
                                        }
                                    }, '删除')
                                ]);
                            }
                        }
                    }
                ],
                initPage:1,
                pageNum:0
            }
        },
        created: function () {
            this.load()
        },
        methods: {
            load (){
                let _this = this
                let params = new URLSearchParams()
                params.append('pno',_this.initPage)
                $http.post('/gonggao/listGonggao',params).then(function (res) {
                    if(res.suc){
                        _this.Message = res.result.list
                        _this.pageNum = res.result.totalRow
                        _this.loading = false
                    }else{
                        if(res.msg == "请先登录"){
                            _this.$Message.error('请重新登录！');
                        }else{
                            _this.$Message.error(res.msg);
                        }
                    }

                })
            },
            pageChange(index){
                this.initPage = index
                this.loading = true
                this.load ()
            },
            info(index){
                let _this = this
                let params = new URLSearchParams()
                params.append('id', index.row.id)
                $http.post('/gonggao/deleteGonggao',params).then(function (res) {
                    if(res.suc){
                        _this.load()
                    }else{
                        _this.$Message.error('删除失败！');
                    }
                })
            },
            addAnn(){
                var changeObj = {
                    'biaoti': '',
                    'zhaiyao': '',
                    'neirong': '',
                    'pic': '',
                    'id': ''
                }
                changeObj = JSON.stringify(changeObj)
                localStorage.setItem('changeInner',changeObj)
                this.$router.push({
                    name: 'artical-publish'
                })
            }
        }
    }
</script>
<style>
</style>

